package com.xnyzc.lhy.common.component.service.impl;

import com.xnyzc.lhy.common.component.mapper.GatewayMapper;
import com.xnyzc.lhy.common.component.service.ICacheService;
import com.xnyzc.lhy.common.component.service.IGatewayService;
import com.xnyzc.lhy.common.entity.OaGatewayIgnore;
import com.xnyzc.lhy.common.entity.PangClient;
import com.xnyzc.lhy.common.entity.PangRoute;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.redisenum.RedisClientEnum;
import com.xnyzc.lhy.common.redisenum.RedisGatewayIgnoreEnum;
import com.xnyzc.lhy.common.redisenum.RedisRouteEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: scootXin
 * @Date: 2019/9/26 21:53
 */
@Slf4j
@Service
public class GatewayServiceImpl implements IGatewayService {
    @Autowired
    private GatewayMapper gatewayMapper;
    @Autowired
    private ICacheService cacheService;

    @Override
    public PangClient getPangClient(String clientId) {

        if (clientId == null) {
            throw PangException.create(EErrorCode.NO_SECRET_KEY, "clientId is null");
        }

        List<PangClient> pangClients = null;

        //初始化数据
        if (!cacheService.check(RedisClientEnum.base)) {
            pangClients = gatewayMapper.getClientSecret();
            cacheService.valAddOrUpdate(RedisClientEnum.base, pangClients);
        }
        //缓存中获取数据
        else {
            pangClients = cacheService.valGetList(RedisClientEnum.base, PangClient.class);
        }

        if (pangClients == null) {
            throw PangException.create(EErrorCode.NO_SECRET_KEY, "no {} found for client", clientId);
        }

        for (PangClient client : pangClients) {
            if (clientId.equals(client.getClientId())) {
                return client;
            }
        }

        throw PangException.create(EErrorCode.NO_SECRET_KEY, "no {} found for client", clientId);
    }

    @Override
    public List<PangRoute> getPangRouteList() {

        List<PangRoute> pangRoutes;

        //初始化数据
        if (!cacheService.check(RedisRouteEnum.base)) {
            pangRoutes = gatewayMapper.getRouteList();
            cacheService.valAddOrUpdate(RedisRouteEnum.base, pangRoutes);
        }
        //缓存中获取数据
        else {
            pangRoutes = cacheService.valGetList(RedisRouteEnum.base, PangRoute.class);
        }

        if (pangRoutes == null || pangRoutes.isEmpty()) {
            throw PangException.create(EErrorCode.dataCheckErr, "not found for PangRoute");
        }

        return pangRoutes;
    }

    @Override
    public List<OaGatewayIgnore> getSignIgnoreList() {

        List<OaGatewayIgnore> oaGatewayIgnore;

        //初始化数据
        if (!cacheService.check(RedisGatewayIgnoreEnum.base)) {
            oaGatewayIgnore = gatewayMapper.getSignIgnoreList();
            cacheService.valAddOrUpdate(RedisGatewayIgnoreEnum.base, oaGatewayIgnore);
        }
        //缓存中获取数据
        else {
            oaGatewayIgnore = cacheService.valGetList(RedisGatewayIgnoreEnum.base, OaGatewayIgnore.class);
        }

        return oaGatewayIgnore;
    }
}
